package com.group4.xiao_ge_lai_yi_zhu.controller.login_controller;

import com.group4.xiao_ge_lai_yi_zhu.MyException.NumberNotCorrectException;
import com.group4.xiao_ge_lai_yi_zhu.domain.BlueBall;
import com.group4.xiao_ge_lai_yi_zhu.domain.Lottery;
import com.group4.xiao_ge_lai_yi_zhu.domain.RedBall;
import com.group4.xiao_ge_lai_yi_zhu.factory.LotteryFactory;
import com.group4.xiao_ge_lai_yi_zhu.service.BuyService;


import java.util.*;


/**
 * 购买界面
 * 用户选择了购买彩票后的页面
 * 实现手动选号和自动选号
 */
public class BuyPage {
    private static Scanner sc = new Scanner(System.in);
    private static Random ra = new Random();


    //彩票序号
    private static int serialNum = 0;
    private BuyService buyService = new BuyService();

    //购买界面
    public void buyLottery() {
        HomePage homePage = new HomePage();
        while (true) {
            System.out.println("1.手动选票");
            System.out.println("2.自动选票");
            System.out.println("3.自动十连抽(抽十次送一次)");
            System.out.println("4.返回");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    //手动选票
                    String num1 = selectManually();
//                System.out.println("1.手动选票");
                    serialNum++;
                    String s = "p" + serialNum;
                    Lottery lottery = LotteryFactory.getLottery(num1, s);
                    System.out.println("序号: " + lottery.getSerialNum() +
                            "    期数: " + lottery.getPeriod() +
                            "    开奖时间:  " + lottery.getPrizeTime() +
                            "    号码: " + lottery.getLotteryNum() +
                            "    出票时期: " + lottery.getTicketTime());
                    buyService.lotteryTransmission(lottery);
                    break;
                case 2:
                    //自动选票
                    String num2 = automaticSelection();
                    serialNum++;
                    String s1 = "p" + serialNum;
//                System.out.println("2.自动选票");
                    Lottery lottery1 = LotteryFactory.getLottery(num2, s1);
                    System.out.println("序号: " + lottery1.getSerialNum() +
                            "    期数: " + lottery1.getPeriod() +
                            "    开奖时间:  " + lottery1.getPrizeTime() +
                            "    号码: " + lottery1.getLotteryNum() +
                            "    出票时期: " + lottery1.getTicketTime());
                    buyService.AutomaticSelection(lottery1);
                    break;
                case 3:
                    for (int i = 0; i < 11; i++) {
                        //自动选票
                        String num3 = automaticSelection();
                        serialNum++;
                        String s2 = "p" + serialNum;
//                System.out.println("2.自动选票");
                        Lottery lottery2 = LotteryFactory.getLottery(num3, s2);
                        System.out.println("序号: " + lottery2.getSerialNum() +
                                "    期数: " + lottery2.getPeriod() +
                                "    开奖时间:  " + lottery2.getPrizeTime() +
                                "    号码: " + lottery2.getLotteryNum() +
                                "    出票时期: " + lottery2.getTicketTime());
                        buyService.AutomaticSelection(lottery2);
                    }
                    break;
                case 4:
                    homePage.page();
                    break;
            }
        }
    }


    //手动动选择票
    public String selectManually() {
        ArrayList<Integer> list = new ArrayList<>();
        System.out.println("输入第" + 1 + "个红数");
        int j = sc.nextInt();
        list.add(j);
        lo:
        for (int i = 1; i < 6; i++) {

            System.out.println("输入第" + (i + 1) + "个红数");
            int i1 = sc.nextInt();
            if (i1 > 33 || i1 < 1) {
                System.out.println("号码超出范围,请重新输入");
                i--;
                continue;
            }
            for (int i2 = 0; i2 < list.size(); i2++) {
                if (i1 == list.get(i2)) {
                    System.out.println("号码重复,请重新输入");
                    i--;
                    continue lo;
                }
            }
            list.add(i1);
        }
        int blue;
        while (true) {
            System.out.println("请输入蓝数");
            blue = sc.nextInt();
            if (blue > 16 || blue < 1) {
                System.out.println("号码超出范围,请重新输入");
                continue;
            }
            break;
        }

        StringBuilder nums = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) < 10) {
                nums.append("0" + list.get(i));
            } else {
                nums.append(list.get(i));
            }
            nums.append(" ");
        }
        if (blue < 10) {
            nums.append("0" + blue);
        } else {
            nums.append(blue);
        }
        String num = nums.toString();
        return num;
//        BufferedWriter bw = null;
//        //TODO
//        //把存储到本地的功能改到数据库类里
//        //存成properties的配置文件
    }


    //自动选择票
    public String automaticSelection() {
        //筛选红球
        HashSet<Integer> redNum = new HashSet<>();
        for (int i = 0; i < 6; i++) {
            int redBallNum = ra.nextInt(33) + 1;
            //system.out.println(redBallNum);
            boolean flag = redNum.add(redBallNum);
            if (!flag) {
                i--;
            }
        }
        ArrayList<RedBall> redBalls = new ArrayList<>();
        for (Integer num : redNum) {
            try {
                redBalls.add(new RedBall(num));
            } catch (NumberNotCorrectException e) {
                e.printStackTrace();
            }
        }
        //筛选篮球
        int blue = ra.nextInt(16) + 1;
        try {
            BlueBall blueBall = new BlueBall(blue);
        } catch (NumberNotCorrectException e) {
            e.printStackTrace();
        }
        //生成彩票号码-->前六个是红球,最后一个是篮球
        StringBuilder nums = new StringBuilder();
        for (int i = 0; i < redBalls.size(); i++) {
            if (redBalls.get(i).getNumber() < 10) {
                nums.append("0" + redBalls.get(i).getNumber() + " ");
            } else {
                nums.append(redBalls.get(i).getNumber() + " ");
            }
        }
        nums.append(" ");
        if (blue < 10) {
            nums.append("0" + blue);
        } else {
            nums.append(blue);
        }
        String num = nums.toString();
        return num;
    }
}
